package com.yaodian100.mall.member.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.ec.giftcard.entity.GiftcardAmount;
import com.yaodian100.ec.giftcard.entity.GiftcardItem;
import com.yaodian100.ec.giftcard.service.GiftcardService;
import com.yaodian100.ec.member.entity.ImageValid;
import com.yaodian100.ec.member.entity.Member;
import com.yaodian100.ec.member.entity.VirtualBalance;
import com.yaodian100.ec.member.service.MemberService;
import com.yaodian100.mall.member.util.UserData;

/**
 * @author aaron.chueh
 * 
 */
public class MyGiftcardAction extends ActionSupport implements Preparable {
	/**
	 * serial version
	 */
	private static final long serialVersionUID = 2267156971881261085L;
	
	private MemberService memberService;
	private GiftcardService giftcardService;
	
	private UserData userData=null;
	private Member member;
	private SimplePager pager;
	private long virtualBalanceSize;
	private List<VirtualBalance> virtualBalances;
	private String cardCode;
	private String cardPassword;
	private String checkCode;
	private int returnCode=-1;

	public void prepare() throws Exception {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
				ServletActionContext.HTTP_RESPONSE);
		userData = new UserData(request, response);
		
		if (pager == null) {
			pager = new SimplePager();
		}
		pager.setPageRecord(10);//设定商品查询每页显示10笔
	}

	public String tomygiftcard() throws Exception {
		member = memberService.get(userData.getMemberId());
		
		virtualBalanceSize=memberService.getGiftcardBalanceSize(userData.getMemberId());
		pager.setTotalSize(virtualBalanceSize);
		virtualBalances=memberService.getGiftcardBalanceByPage(userData.getMemberId(),pager);
		
		return "tomygiftcard";
	}
	
	public String giftcardList() throws Exception {
		member = memberService.get(userData.getMemberId());
		
		virtualBalanceSize=memberService.getGiftcardBalanceSize(userData.getMemberId());
		pager.setTotalSize(virtualBalanceSize);
		virtualBalances=memberService.getGiftcardBalanceByPage(userData.getMemberId(),pager);
		
		return "giftcardList";
	}
	
	public String rechargeAdd() throws Exception {
		
		return "rechargeAdd";
	}
	
	public String recharge() throws Exception {
		ImageValid imageValid=new ImageValid();
		imageValid.setId(userData.getImageValid());
		imageValid.setValidData(checkCode);
		try{
			memberService.isImageValid(imageValid);
		}catch(Exception e){
			returnCode=1;
			return "rechargeAdd";
		}
		
		if(!this.checkCardCodeFormat(cardCode) || !this.checkCardPasswordFormat(cardPassword)){
			returnCode=2;
			return "rechargeAdd";
		}
		
		int code=giftcardService.checkCardCode(cardCode,cardPassword);
		if(code==1){
			returnCode=2;
			return "rechargeAdd";
		}else if(code==2){
			returnCode=3;
			return "rechargeAdd";
		}else if(code==3){
			returnCode=4;
			return "rechargeAdd";
		}else if(code==4){
			returnCode=5;
			return "rechargeAdd";
		}
				
		Date now=new Date();
		GiftcardAmount giftcardAmount=giftcardService.getGiftcardAmountByCardCode(cardCode);
		
		VirtualBalance vb=new VirtualBalance();
		vb.setMemberId(userData.getMemberId());
		vb.setTrxType("EC_GIFTCARD_MAIN");
		vb.setTrxTypeId(cardCode);
		vb.setTrxAmount(giftcardAmount.getAmount());
		vb.setTrxDate(now);
		vb.setDescription("礼品卡券号"+cardCode+"充值");
		vb.setType("G");
		
		GiftcardItem giftcardItem=giftcardService.getGiftcardItemByCardCode(cardCode);
		giftcardItem.setRechargeStatus("1");
		giftcardItem.setRechargeDate(now);
		giftcardItem.setRechargeIp(ServletActionContext.getRequest().getRemoteAddr());
		giftcardItem.setMemberId(userData.getMemberId());
		
		giftcardService.rechargeGiftcard(vb,giftcardItem);
		returnCode=0;
		
		member = memberService.get(userData.getMemberId());
		
		cardCode=null;
		cardPassword=null;
		
		return "rechargeAdd";
	}
	
	private boolean checkCardCodeFormat(String cardCode){
		if(cardCode.length()!=11)
			return false;
		
		if(!cardCode.matches("\\d+"))
			return false;
		
		return true;
	}
	
	private boolean checkCardPasswordFormat(String cardPassword){
		if(cardPassword.length()!=8)
			return false;
		
		if(!cardPassword.matches("^[A-Z0-9]+$"))
			return false;
		
		return true;
	}

	//getter & setter
	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public GiftcardService getGiftcardService() {
		return giftcardService;
	}

	public void setGiftcardService(GiftcardService giftcardService) {
		this.giftcardService = giftcardService;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public SimplePager getPager() {
		return pager;
	}

	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	public long getVirtualBalanceSize() {
		return virtualBalanceSize;
	}

	public void setVirtualBalanceSize(long virtualBalanceSize) {
		this.virtualBalanceSize = virtualBalanceSize;
	}

	public List<VirtualBalance> getVirtualBalances() {
		return virtualBalances;
	}

	public void setVirtualBalances(List<VirtualBalance> virtualBalances) {
		this.virtualBalances = virtualBalances;
	}

	public String getCardCode() {
		return cardCode;
	}

	public void setCardCode(String cardCode) {
		this.cardCode = cardCode;
	}

	public String getCardPassword() {
		return cardPassword;
	}

	public void setCardPassword(String cardPassword) {
		this.cardPassword = cardPassword;
	}

	public String getCheckCode() {
		return checkCode;
	}

	public void setCheckCode(String checkCode) {
		this.checkCode = checkCode;
	}

	public int getReturnCode() {
		return returnCode;
	}

	public void setReturnCode(int returnCode) {
		this.returnCode = returnCode;
	}
}